package com.dhcc.core.framework.result.node;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.constant.state.ResourceType;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.SpringContextHolder;

/**
 * 菜单的节点
 * 
 * @ClassName: ZTreeNodeMenu
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:30:01
 */
@SuppressWarnings("rawtypes")
public class ZTreeNodeMenu implements Comparable, Serializable{

    /**
     * @fieldName: serialVersionUID
     * @fieldType: long
     * @Description: TODO
     */
    private static final long serialVersionUID = 1L;
    /**
     * 节点id
     */
    private Long id;

    /**
     * 父节点
     */
    private Long pid;

    /**
     * 节点名称
     */
    private String text;

    /**
     * 资源级别
     */
    private Integer levels;

    /**
     * 资源类别
     */
    private Integer restype;

    /**
     * 资源的排序
     */
    private Integer weight;

    /**
     * 节点的url
     */
    private String url;

    /**
     * 节点图标
     */
    private String iconCls;

    /**
     * 子节点的集合
     */
    private List<ZTreeNodeMenu> children;

    /**
     * 查询子节点时候的临时集合
     */
    private List<ZTreeNodeMenu> linkedList = new ArrayList<ZTreeNodeMenu>();

    public ZTreeNodeMenu() {
        super();
    }

    public ZTreeNodeMenu(Long id, Long pid) {
        super();
        this.id = id;
        this.pid = pid;
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public static ZTreeNodeMenu createRoot() {
        return new ZTreeNodeMenu(0l, -1l);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<ZTreeNodeMenu> getChildren() {
        return children;
    }

    public void setChildren(List<ZTreeNodeMenu> children) {
        this.children = children;
    }

    public Integer getRestype() {
        return restype;
    }

    public void setRestype(Integer restype) {
        this.restype = restype;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "ResourceNode{" + "id=" + id + ", pid=" + pid + ", text='" + text + '\'' + ", levels=" + levels
                + ", weight=" + weight + ", url='" + url + '\'' + ", iconCls='" + iconCls + '\'' + ", children=" + children
                + ", linkedList=" + linkedList + '}';
    }

    @Override
    public int compareTo(Object o) {
        ZTreeNodeMenu menuNode = (ZTreeNodeMenu) o;
        Integer weight = menuNode.getWeight();
        if (weight == null) {
            weight = 0;
        }
        if (this.weight == null) {
            this.weight = 0;
        }
        return this.weight.compareTo(weight);
    }

    /**
     * 构建整个菜单树
     *
     * @author cyf
     */
    public void buildNodeTree(List<ZTreeNodeMenu> nodeList) {
        for (ZTreeNodeMenu treeNode : nodeList) {
            List<ZTreeNodeMenu> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
            if (linkedList.size() > 0) {
                treeNode.setChildren(linkedList);
            }
        }
    }

    /**
     * 查询子节点的集合
     *
     * @author cyf
     */
    public List<ZTreeNodeMenu> findChildNodes(List<ZTreeNodeMenu> nodeList, Long pid) {
        if (nodeList == null && pid == null) {
            return null;
        }
        for (Iterator<ZTreeNodeMenu> iterator = nodeList.iterator(); iterator.hasNext();) {
            ZTreeNodeMenu node = (ZTreeNodeMenu) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (CommonUtil.isNotEmpty(node.getPid())&&node.getPid() != 0 && pid.equals(node.getPid())) {
                recursionFn(nodeList, node, pid);
            }
        }
        return linkedList;
    }

    /**
     * 遍历一个节点的子节点
     *
     * @author cyf
     */
    public void recursionFn(List<ZTreeNodeMenu> nodeList, ZTreeNodeMenu node, Long pId) {
        List<ZTreeNodeMenu> childList = getChildList(nodeList, node);// 得到子节点列表
        if (childList.size() > 0) {// 判断是否有子节点
            if (node.getPid().equals(pId)) {
                linkedList.add(node);
            }
            Iterator<ZTreeNodeMenu> it = childList.iterator();
            while (it.hasNext()) {
                ZTreeNodeMenu n = (ZTreeNodeMenu) it.next();
                recursionFn(nodeList, n, pId);
            }
        } else {
            if (node.getPid().equals(pId)) {
                linkedList.add(node);
            }
        }
    }

    /**
     * 得到子节点列表
     *
     * @author cyf
     */
    private List<ZTreeNodeMenu> getChildList(List<ZTreeNodeMenu> list, ZTreeNodeMenu node) {
        List<ZTreeNodeMenu> nodeList = new ArrayList<ZTreeNodeMenu>();
        Iterator<ZTreeNodeMenu> it = list.iterator();
        while (it.hasNext()) {
            ZTreeNodeMenu n = (ZTreeNodeMenu) it.next();
            if (n.getPid().equals(node.getId())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }

    /**
     * 清除掉权限级别的资源
     *
     * @date 2017年2月19日 下午11:04:11
     */
    public static List<ZTreeNodeMenu> clearBtn(List<ZTreeNodeMenu> nodes) {
        ArrayList<ZTreeNodeMenu> noBtns = new ArrayList<ZTreeNodeMenu>();
        SysProperties sysProperties = SpringContextHolder.getBean(SysProperties.class);
        for (ZTreeNodeMenu node : nodes) {
            if (node.getRestype() == ResourceType.CATALOG.getCode()
                    || node.getRestype() == ResourceType.MENU.getCode()) {
                // 复制一个对象，否则会直接改缓存，而且每次都改，那就麻烦了
                ZTreeNodeMenu _tmp = new ZTreeNodeMenu();
                BeanUtils.copyProperties(node, _tmp);
                // 排除接口文档
                if (node.getUrl() != null&&!(Const.API_MENU_URL.equals(node.getUrl()) || Const.API_DRUID_URL.equals(node.getUrl()))) {
                    // 以#开头不加backendPath
                    if(node.getUrl().indexOf("#")==0){
                        _tmp.setUrl(node.getUrl().substring(1));
                    }else{
                        // 默认添加 backendPath
                        _tmp.setUrl(sysProperties.getBackendPath() + node.getUrl());
                    }
                }
                noBtns.add(_tmp);
            }
        }
        return noBtns;
    }

    /**
     * 清除所有二级菜单
     *
     * @date 2017年2月19日 下午11:18:19
     */
    public static List<ZTreeNodeMenu> clearLevelTwo(List<ZTreeNodeMenu> nodes) {
        ArrayList<ZTreeNodeMenu> results = new ArrayList<ZTreeNodeMenu>();
        for (ZTreeNodeMenu node : nodes) {
            Integer levels = node.getLevels();
            if (levels.equals(1)) {
                results.add(node);
            }
        }
        return results;
    }

    /**
     * 构建菜单列表
     *
     * @date 2017年2月19日 下午11:18:19
     */
    public static List<ZTreeNodeMenu> buildMenu(List<ZTreeNodeMenu> nodes) {
        List<ZTreeNodeMenu> clearBtn = clearBtn(nodes);
        new ZTreeNodeMenu().buildNodeTree(clearBtn);
        List<ZTreeNodeMenu> menuNodes = clearLevelTwo(clearBtn);
        /*
        // 对菜单排序
        Collections.sort(menuNodes);

        // 对菜单的子菜单进行排序
        for (ResourceNode menuNode : menuNodes) {
            if (menuNode.getChildren() != null && menuNode.getChildren().size() > 0) {
                Collections.sort(menuNode.getChildren());
            }
        }
        */
        // 如果关闭了接口文档,则不显示接口文档菜单
        SysProperties sysProperties = SpringContextHolder.getBean(SysProperties.class);
        if (!sysProperties.getSwaggerOpen()) {
            List<ZTreeNodeMenu> menuNodesCopy = new ArrayList<>();
            for (ZTreeNodeMenu menuNode : menuNodes) {
                if (Const.API_MENU_URL.equals(menuNode.getUrl())) {
                    continue;
                } else {
                    menuNodesCopy.add(menuNode);
                }
            }
            menuNodes = menuNodesCopy;

        }

        return menuNodes;
    }
}
